import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;

public class CapnArnav extends Random {

	public int startValue3;
	public int startValue2;
	public int startValue1;

	public int currValue3;
	public int currValue2;
	public int currValue1;

	public int thirdCoins;
	public int secondCoins;
	public int firstCoins;

	private boolean thirdColFound = false;
	private boolean secondColFound = false;
	private boolean firstColFound = false;

	private boolean reel1done = false;
	private boolean reel2done = false;
	private boolean reel3done = false;

	public int captainID = 2308;
	public int i = 0;

	boolean done = false;

	@Override
	public boolean activateCondition() {

		final RSNPC captain = getNearestNPCByName("Cap'n Arnav");

		if (captain != null) {
			return true;
		}

		return false;
	}

	@Override
	public String getAuthor() {
		return "Keilgo";
	}

	@Override
	public String getName() {
		return "Cap'n Arnav";
	}

	@Override
	public double getVersion() {
		return 0.1;
	}

	@Override
	public int loop() {

		final RSNPC captain = getNearestNPCByID(captainID);

		if (captain == null) {
			return -1;
		}

		if (getMyPlayer().isMoving() || getMyPlayer().getAnimation() != -1) {
			return random(1200, 1500);
		}

		if (done != true) {
			done = searchText(241, "hop through")
					|| searchText(242, "hop through");
		}

		if (done) {

			final RSObject Portal = findObject(11369);

			if (Portal == null) {
				System.out.println("Can't find portal!");
				return random(800, 1200);
			}

			atObject(Portal, "Enter");
			return random(800, 1200);
		}

		if (interfaceExists(185)) {
			log("Setting default position. Coins Coins Coins!");
			while (firstColFound != true) {
				while (thirdColFound == false) {
					startValue3 = getSetting(809);
					wait(random(500, 700));
					clickMouse(random(295, 315), random(13, 33), true);
					wait(random(800, 1000));
					currValue3 = getSetting(809);
					if (currValue3 < startValue3) {
						thirdColFound = true;
					}
				}

				while (secondColFound == false) {
					startValue2 = getSetting(809);
					wait(random(500, 700));
					clickMouse(random(203, 223), random(13, 33), true);
					wait(random(800, 1000));
					currValue2 = getSetting(809);
					if (currValue2 < startValue2) {
						secondColFound = true;
					}
				}

				while (firstColFound == false) {
					startValue1 = getSetting(809);
					wait(random(500, 700));
					clickMouse(random(117, 137), random(13, 33), true);
					wait(random(800, 1000));
					currValue1 = getSetting(809);
					if (currValue1 < startValue1) {
						firstColFound = true;
					}
				}
			}
		}

		if (interfaceExists(185)) {
			if (searchText(185, "Bar")) {
				while (reel1done == false) {
					clickMouse(random(117, 137), random(13, 33), true);
					wait(random(800, 1000));
					clickMouse(random(117, 137), random(13, 33), true);
					wait(random(800, 1000));
					log("Reel 1 Bar Found!");
					reel1done = true;
				}

				while (reel2done == false) {
					clickMouse(random(203, 223), random(13, 33), true);
					wait(random(800, 1000));
					clickMouse(random(203, 223), random(13, 33), true);
					wait(random(800, 1000));
					log("Reel 2 Bar Found!");
					reel2done = true;
				}

				while (reel3done == false) {
					clickMouse(random(295, 315), random(13, 33), true);
					wait(random(800, 1000));
					clickMouse(random(295, 315), random(13, 33), true);
					wait(random(800, 1000));
					log("Reel 3 Bar Found!");
					reel3done = true;
				}

				if (reel3done == true) {
					if (RSInterface.getInterface(185).isValid()) {
						atInterface(RSInterface.getInterface(185).getChild(28));
						wait(random(700, 1000));
					}
				}
			}
		}

		if (interfaceExists(185)) {
			if (searchText(185, "Coins")) {
				while (reel1done == false) {
					log("Reel 1 Coins Found!");
					reel1done = true;
				}

				while (reel2done == false) {
					log("Reel 2 Coins Found!");
					reel2done = true;
				}

				while (reel3done == false) {
					log("Reel 3 Coins Found!");
					reel3done = true;
				}

				if (reel3done == true) {
					if (RSInterface.getInterface(185).isValid()) {
						atInterface(RSInterface.getInterface(185).getChild(28));
						wait(random(700, 1000));
					}
				}
			}
		}

		if (interfaceExists(185)) {
			if (searchText(185, "Bowl")) {
				while (reel1done == false) {
					clickMouse(random(117, 137), random(13, 33), true);
					wait(random(800, 1000));
					log("Reel 1 Bowl Found!");
					reel1done = true;
				}

				while (reel2done == false) {
					clickMouse(random(203, 223), random(13, 33), true);
					wait(random(800, 1000));
					log("Reel 2 Bowl Found!");
					reel2done = true;
				}

				while (reel3done == false) {
					clickMouse(random(295, 315), random(13, 33), true);
					wait(random(800, 1000));
					log("Reel 3 Bowl Found!");
					reel3done = true;
				}

				if (reel3done == true) {
					if (RSInterface.getInterface(185).isValid()) {
						atInterface(RSInterface.getInterface(185).getChild(28));
						wait(random(700, 1000));
					}
				}
			}
		}

		if (interfaceExists(185)) {
			if (searchText(185, "Ring")) {
				while (reel1done == false) {
					clickMouse(random(117, 137), random(296, 316), true);
					wait(random(800, 1000));
					log("Reel 1 Ring Found!");
					reel1done = true;
				}

				while (reel2done == false) {
					clickMouse(random(203, 223), random(296, 316), true);
					wait(random(800, 1000));
					log("Reel 2 Ring Found!");
					reel2done = true;
				}

				while (reel3done == false) {
					clickMouse(random(295, 315), random(296, 316), true);
					wait(random(800, 1000));
					log("Reel 3 Ring Found!");
					reel3done = true;
				}

				if (reel3done == true) {
					if (RSInterface.getInterface(185).isValid()) {
						atInterface(RSInterface.getInterface(185).getChild(28));
						wait(random(700, 1000));
					}
				}
			}
		}

		if (interfaceExists(228)) {
			final int x = random(220, 310), y = random(427, 437);
			clickMouse(x, y, true);
		}

		if (searchText(241, "yer foot")) {
			final RSObject Chest = findObject(42337);
			atObject(Chest, "Open");
			return random(800, 1200);
		}

		if (!myClickContinue()) {
			atNPC(captain, "Talk-to");
		}
		return random(1000, 1500);
	}

	public boolean myClickContinue() {
		wait(random(800, 1000));
		return atInterface(243, 7) || atInterface(241, 5)
				|| atInterface(242, 6) || atInterface(244, 8)
				|| atInterface(64, 5);
	}

	public boolean searchText(final int interfac, final String text) {
		final RSInterface talkFace = getInterface(interfac);
		if (!talkFace.isValid()) {
			return false;
		}
		for (int i = 0; i < talkFace.getChildCount(); i++) {
			if (talkFace.getChild(i).containsText(text)) {
				return true;
			}
		}

		return false;
	}
}